﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Zebra.WinFramework
{
    public partial class BaseCRUDForm : FormUC
    {
        public event EventHandler SaveClick;
        public event EventHandler DeleteClick;
        public event EventHandler NewClick;
        public event EventHandler ResetClick;
        public event EventHandler DataRowSelected;
        public event EventHandler GridViewSearchClick;
        public event EventHandler TextChangedSearch;

        private object _selectedItem;
        public object SelectedItem
        {
            get { return _selectedItem; }
        }

        public object Datasource { get; set; }
        public object Object { get; set; }

        public BaseCRUDForm()
        {
            InitializeComponent();
            Initialize();
        }
        private void Initialize()
        {
            this.BaseGridviewSearch.lblCount.Text = "0"; //todo
        }

        private void BaseButton_SaveClick(object sender, EventArgs e)
        {
            if (null != SaveClick)
                SaveClick(sender, e);
        }
        private void BaseButton_DeleteClick(object sender, EventArgs e)
        {
            if (BaseGridviewSearch.BaseGridview.gvMain.Rows.Count == 0
              || BaseGridviewSearch.BaseGridview.gvMain.CurrentRow.Selected == false)
            {
                MessageBox.Show("Please select a row to delete", "Select a row", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (null != DeleteClick)
                DeleteClick(sender, e);
        }
        private void BaseButton_NewClick(object sender, EventArgs e)
        {
            if (null != NewClick)
                NewClick(sender, e);
        }
        private void BaseButton_ResetClick(object sender, EventArgs e)
        {
            if (null != ResetClick)
                ResetClick(sender, e);
        }


        private void BaseGridViewSearch_DataRowSelected(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                _selectedItem = BaseGridviewSearch.BaseGridview.gvMain.Rows[e.RowIndex].DataBoundItem;

            if (DataRowSelected != null)
                DataRowSelected(_selectedItem, e);
        }
        private void BaseGridViewSearch_SearchClick(object sender, EventArgs e)
        {
            // GridViewSearchClick(sender, e);
        }
        private void BaseGridViewSearch_TextChangedSearch(object sender, EventArgs e)
        {
            string s = BaseGridviewSearch.txtSearch.Text.Trim().ToLower();
            //if (s.Length > 0)
            // {
            if (null != TextChangedSearch)
                TextChangedSearch(s, e);
            //}
        }

    }
}
